package com.bc.commoncore.util;

import com.bc.commoncore.config.ConfigBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class SystemUtils {
	private volatile static   List<String> localIpInfo;
	private final  static Logger log = LoggerFactory.getLogger(SystemUtils.class);
	/**
	 * 获取本机的内网ip，本机名，外网ip
	 * @date 20151022
	 * @author zhaohl
	 * @return
	 * @throws Exception 
	 */
	public static List<String> getLocalIpInfo() {
		if (localIpInfo == null) {
			synchronized (SystemUtils.class) {
				if (localIpInfo == null) {
					localIpInfo = getLocalInfo();
				}
			}
		}
		return localIpInfo;
	}

	public static String getLocalIp() {
		List<String> ipinfoList = getLocalIpInfo();
		if (!ipinfoList.get(2).isEmpty()) {
			return ipinfoList.get(2);
		}
		if (!ipinfoList.get(0).isEmpty()) {
			return ipinfoList.get(0);
		}
		return "";
	}
	private static List<String> getLocalInfo(){
		String localip = null;// 本地IP，如果没有配置外网IP则返回它  
        String netip = null;// 外网IP  
        String hostName = null;// 本机名称
        try {  
            InetAddress ip = null;  
            boolean finded = false;// 是否找到外网IP  
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();  
            while (netInterfaces.hasMoreElements() && !finded) {  
                NetworkInterface ni = netInterfaces.nextElement();  
                Enumeration<InetAddress> address = ni.getInetAddresses();  
                while (address.hasMoreElements()) {  
                    ip = address.nextElement();  
                    if (!ip.isLoopbackAddress()&&ip.getHostAddress().indexOf(":") == -1) {
						if (ip.isSiteLocalAddress()) {
	                        localip = ip.getHostAddress();// 内网IP 
	                        hostName = ip.getHostName();
						}else {
	                        netip = ip.getHostAddress();// 外网IP  
	                        finded = true;  
	                        break; 
						}
					}
                }  
            }  
        } catch (Exception e) {  
        	log.error("获取本机ip时异常:"+e.getMessage());
        } 
        if (localip==null) {
        	localip="";
		}
        if (netip==null) {
        	netip="";
		}
        if (hostName==null) {
        	hostName="";
		}
        List<String> listInfo = new ArrayList<>();
        listInfo.add(localip);
        listInfo.add(hostName);
        listInfo.add(netip);
		return listInfo;
	}
	public static String getClientIp(HttpServletRequest request){
		if (request==null) {
			return "";
		}
		return IPUtil.getIpAddr(request);
	}
	/**
	 * 发送邮件
	 * @param title
	 * @param body
	 * @param emailHost
	 * @param emailFrom
	 * @param emailTo
	 * @param emailUserName
	 * @param emailPwd
	 */
	public static boolean sendEmailTo(String title,String body,String emailHost,String emailFrom,
			String emailTo,String emailUserName,String emailPwd){
		try {
			if (!ConfigBase.getIsSendMail().equals("1")) {
				return false;
			}
			if (emailTo==null||emailTo.trim().isEmpty()) {
				return false;
			}
		    Properties props = new Properties();
		    // Setup mail server
		    //否则发送邮件时，可能会报错： 501 Syntax: HELO hostname
		    props.put("mail.smtp.localhost", "localHostAdress"); 
		    props.put("mail.smtp.host", emailHost);// 设置smtp主机
		    props.put("mail.smtp.auth", "true");// 使用smtp身份验证
		    Session session = Session.getDefaultInstance(props, null);
		    MimeMessage message = new MimeMessage(session);
		    message.setFrom(new InternetAddress(emailFrom));
			String split = "";
			if (emailTo.indexOf(";") > 0) {
				split = ";";
			} else if (emailTo.indexOf(",") > 0) {
				split = ",";
			}
		    if (!split.isEmpty()) {
		    	String [] mailtoStrings=emailTo.split(split);
		    	List<InternetAddress> toList=new ArrayList<>();
		    	for (String string : mailtoStrings) {
					if (string!=null&&!string.trim().isEmpty()) {
						toList.add(new InternetAddress(string));
					}
				}
		    	message.addRecipients(Message.RecipientType.TO, toList.toArray(new InternetAddress[toList.size()]));
			}else {
				  message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
			}
		    message.setSubject(title, "utf-8");//标题
		    message.setText(body,"utf-8");//内容
		    //http协议部分会自动转换成超链接
		    message.saveChanges();
		    // Send message
		    Transport transport = session.getTransport("smtp");
		    transport.connect(emailHost, emailUserName, emailPwd);
		    transport.sendMessage(message, message.getAllRecipients());
		} catch (Exception e) {
			log.error(title+"发送邮件时异常:"+e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 发送报警邮件
	 * @param title
	 * @param body
	 */
	public static boolean sendAlertEmail(String title,String body){
		String emailTo = ConfigBase.getEmailTo();//"zhao.hailong@etao.cn";
		return sendAlertEmail(title,body,emailTo);
	}
	public static boolean sendAlertEmail(String title,String body,String emailTo){
		String emailHost = ConfigBase.getEmailHost();//"smtp.263.net";//发送邮件服务器
	    String emailFrom = ConfigBase.getEmailFrom();//"edssys@etao.cn";
	    String emailUserName = ConfigBase.getEmailUserName();//"edssys@etao.cn";
	    String emailPwd = ConfigBase.getEmailPwd();//"eds1234";
	    return sendEmailTo(title,body,emailHost,emailFrom,emailTo,emailUserName,emailPwd);
	}
	public static String getSitePath() {
		String result="";
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String staticUrl=request.getRequestURL().toString();
		if (staticUrl.indexOf("http://localhost")==0) {
			String mkkString = staticUrl.substring("http://".length());
			int index = mkkString.indexOf("/");
			if(index>0){
				String url = mkkString.substring(index + 1);
				int end = url.indexOf("/");
				result= staticUrl.substring(0, "http://".length()+index+end+1);
			}
		}else {
			result="http://"+request.getHeader("host");
		}
		return result;
	}
	/**
	 * 判断请求是否为ajax请求
	 * @param request
	 * @return
	 */
	public static boolean isAjax(HttpServletRequest request){
		String requestType = request.getHeader("X-Requested-With");
		if (requestType != null && requestType.equals("XMLHttpRequest")) {
			return true;
		}
		String ajaxParam = request.getParameter("reqType");
		if (ajaxParam!=null&&ajaxParam.equals("ajax")) {
			return true;
		}
		String callback = request.getParameter("callback");
		if (callback!=null&&!callback.equals("")) {
			return true;
		}
		return false;
	}
	
	/**
	 * 发送html格式的邮件
	 * 
	 * @author houzhangbo
	 * @param title
	 * @param body
	 * @param emailHost
	 * @param emailFrom
	 * @param emailTo
	 * @param emailUserName
	 * @param emailPwd
	 * @return
	 */
	public static boolean sendHtmlMail(String title,String body,String emailHost,String emailFrom,
			String emailTo,String emailUserName,String emailPwd){
		
		try {
			if (!ConfigBase.getIsSendMail().equals("1")) {
				return false;
			}
			if (emailTo==null||emailTo.isEmpty()) {
				return false;
			}
		    Properties props = new Properties();
		    // Setup mail server
		    //否则发送邮件时，可能会报错： 501 Syntax: HELO hostname
		    props.put("mail.smtp.localhost", "localHostAdress"); 
		    props.put("mail.smtp.host", emailHost);// 设置smtp主机
		    props.put("mail.smtp.auth", "true");// 使用smtp身份验证
		    Session session = Session.getDefaultInstance(props, null);
		    MimeMessage message = new MimeMessage(session);
		    message.setFrom(new InternetAddress(emailFrom));
		  
		    if (emailTo.indexOf(";")>0) {
		    	String [] mailtoStrings=emailTo.split(";");
		    	List<InternetAddress> toList=new ArrayList<>();
		    	for (String string : mailtoStrings) {
					if (string!=null&&!string.trim().isEmpty()) {
						toList.add(new InternetAddress(string));
					}
				}
		    	message.addRecipients(Message.RecipientType.TO, toList.toArray(new InternetAddress[toList.size()]));
			}else {
				  message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
			}
		    
		    
		    // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
	        Multipart mainPart = new MimeMultipart();
	        // 创建一个包含HTML内容的MimeBodyPart
	        BodyPart html = new MimeBodyPart();
	        // 设置HTML内容
	        html.setContent(body, "text/html; charset=utf-8");
	        mainPart.addBodyPart(html);
		    message.setSubject(title, "utf-8");//标题
	        // 将MiniMultipart对象设置为邮件内容
	        message.setContent(mainPart);
		    Transport transport = session.getTransport("smtp");
		    transport.connect(emailHost, emailUserName, emailPwd);
		    transport.sendMessage(message, message.getAllRecipients());
		} catch (Exception e) {
			log.error(title+"发送邮件时异常:"+e.getMessage());
			return false;
		}
		return true;
    }
	
	/**
	 * 发送html格式的报警邮件
	 * 
	 * @author houzhangbo
	 * @param title
	 * @param body
	 */
	public static boolean sendAlertHtmlEmail(String title,String body){
		String emailTo = ConfigBase.getEmailTo();//"zhao.hailong@etao.cn";
		String emailHost = ConfigBase.getEmailHost();//"smtp.263.net";//发送邮件服务器
	    String emailFrom = ConfigBase.getEmailFrom();//"edssys@etao.cn";
	    String emailUserName = ConfigBase.getEmailUserName();//"edssys@etao.cn";
	    String emailPwd = ConfigBase.getEmailPwd();//"eds1234";
	    return sendHtmlMail(title,body,emailHost,emailFrom,emailTo,emailUserName,emailPwd);
	}
}
